<?php

/**
 * @abstract  微信支付
 * @param datatype paramname
 * @author Joho
 */

namespace Api\Controller;

use Think\Controller;
use Think\Vender;

class WxpayController extends Controller {

    //微信支付入口
    function wxpay() {
       
        $orderid = $_REQUEST['orderid']; //订单ID
        $redirect_url = $_GET['redirect_url']; //回调地址
        $data = getOrderById('Order', array('id' => $orderid), 'amount_goods,amount_goods2,amount_express,tags,status'); //查询订单信息
        //会员非会员价格
        $aprice = $_GET['member'] ? $data['amount_goods'] : $data['amount_goods2'];
        $price = (($aprice + $data['amount_express']) * 100);

        if ($redirect_url) {
            session('redirect_url', $redirect_url); //存地址
        }
        if ($orderid && 1 == $data['status']) {
            if ($price < 0) {
                $this->assign('message', "支付出现异常,请重新支付!");
                $this->display('wxpay-error');
            }
            // echo $price;
            Vendor("wxpay.lib.JsApiPay");
            $tools = new \JsApiPay();
            $t = new \WxPayApi();
            $input = new \WxPayUnifiedOrder();
            $input->SetBody("购买:" . $data['tags']); //购买标题
            $input->SetAttach("test2");
            $input->SetOut_trade_no($orderid); //订单ID
            $input->SetTotal_fee($price); //价额
            $input->SetTime_start(date("YmdHis"));
            $input->SetTime_expire(date("YmdHis", time() + 600));
            $input->SetGoods_tag("test3");
            $input->SetNotify_url("http://a.forcent.cn/ldh/api.php/Wxpay/notify"); //支付回调地址
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid(wx_openid());
            $order = $t->unifiedOrder($input);
            if ($order['err_code'] == "OUT_TRADE_NO_USED") {
                $this->assign('message', "商户订单号不能重复使用,请重新购买!");
                $this->display('wxpay-error');
            } else {
                $jsApiParameters = $tools->GetJsApiParameters($order);
            }


            $this->assign('jsApiParameters', $jsApiParameters);
            $this->assign('orderid', $orderid);
            $this->display();
        } else {
            $this->assign('message', "订单已失效,请重新购买!");
            $this->display('wxpay-error');
        }
    }

    //显示模板
    function show() {
        $action = $_GET['action'];
        $orderid = $_GET['orderid'];
        if (!$orderid || 1 != $action) {
            updateOrderPayInfoById('Order', array('id' => $orderid), array('status' => 3)); //更新订单状态
            $this->assign('message', '支付已取消');
            $this->display('wxpay-error');
        }
        if (1 == $action) {
            redirect(session('redirect_url'));
        }
    }

    //回调函数
    function notify() {
        Vendor("wxpay.lib.WxPayNotify");
        $notify = new \WxPayNotify();
        $data = $notify->Handle(false);
        if ($data['return_code'] == 'SUCCESS') {
            file_put_contents("./paylogs/order_" . $data['out_trade_no'] . ".txt", var_export($data, true));
            updateOrderPayInfoById('Order', array('id' => $data['out_trade_no']), array('status' => 2, 'paytime' => getSysTime())); //更新订单状态
            updateOrderPayInfoById('Order', array('id' => $data['out_trade_no']), array('payinfo' => json_encode($data))); //插入微信订单信息
            updateOrder($data['out_trade_no']);
        }
    }
    
    
    
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //微信支付入口
    function score_wxpay() {
        $orderid = $_REQUEST['orderid']; //订单ID
        $redirect_url = $_GET['redirect_url']; //回调地址
        $data = getScoreOrderById('Score_order', array('id' => $orderid), 'title,price,score,status'); //查询订单信息
        //echo $redirect_url;exit;
        if ($redirect_url) {
            session('redirect_url', $redirect_url); //存地址
        }
        if ($orderid && 1 == $data['status']) {
            if ($data['price'] < 0) {
                $this->assign('message', "支付出现异常,请重新支付!");
                $this->display('Wxpay:wxpay-error');
            }
            // echo $price;
            Vendor("wxpay.lib.JsApiPay");
            $tools = new \JsApiPay();
            $t = new \WxPayApi();
            $input = new \WxPayUnifiedOrder();
            $input->SetBody("购买:" . $data['title']); //购买标题
            $input->SetAttach("test2");
            $input->SetOut_trade_no($orderid); //订单ID
            $input->SetTotal_fee($data['price']*100); //价额
            $input->SetTime_start(date("YmdHis"));
            $input->SetTime_expire(date("YmdHis", time() + 600));
            $input->SetGoods_tag("test3");
            $input->SetNotify_url("http://a.forcent.cn/ldh/api.php/Wxpay/score_notify"); //支付回调地址
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid(wx_openid());
            $order = $t->unifiedOrder($input);
            if ($order['err_code'] == "OUT_TRADE_NO_USED") {
                $this->assign('message', "商户订单号不能重复使用,请重新购买!");
                $this->display('Wxpay:wxpay-error');
            } else {
                $jsApiParameters = $tools->GetJsApiParameters($order);
            }


            $this->assign('jsApiParameters', $jsApiParameters);
            $this->assign('orderid', $orderid);
            $this->display('Wxpay:wxpay');
        } else {
            $this->assign('message', "订单已失效,请重新购买!");
            $this->display('Wxpay:wxpay-error');
        }
    }

    //显示模板
    function score_show() {
        $action = $_GET['action'];
        $orderid = $_GET['orderid'];
        if (!$orderid || 1 != $action) {
            updateOrderPayInfoById('Score_order', array('id' => $orderid), array('status' => 3)); //更新订单状态
            $this->assign('message', '支付已取消');
            $this->display('Wxpay:wxpay-error');
        }
        if (1 == $action) {
            redirect(session('redirect_url'));
        }
    }

    //回调函数
    function score_notify() {
        Vendor("wxpay.lib.WxPayNotify");
        $notify = new \WxPayNotify();
        $data = $notify->Handle(false);
        if ($data['return_code'] == 'SUCCESS') {
            file_put_contents("./paylogs/order_" . $data['out_trade_no'] . ".txt", var_export($data, true));
            updateScoreOrderById('Score_order', array('id' => $data['out_trade_no']), array('status' => 2, 'paytime' => getSysTime())); //更新订单状态
            updateScoreOrderById('Score_order', array('id' => $data['out_trade_no']), array('payinfo' => json_encode($data))); //插入微信订单信息
            //updateOrder($data['out_trade_no']);
            updateHuiyuanScoreById($data['out_trade_no']);
        }
    }
    

}

?>